package com.bjp.image.process;

import java.io.File;
import java.util.Date;

import javax.portlet.ActionRequest;
import javax.servlet.http.HttpServletRequest;

import com.bjp.image.model.ImageReady;
import com.bjp.image.model.impl.ImageReadyImpl;
import com.bjp.image.service.ImageReadyLocalServiceUtil;
import com.bjp.seekersprofile.model.Profile;
import com.liferay.portal.kernel.image.ImageBag;
import com.liferay.portal.kernel.image.ImageProcessorUtil;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.upload.UploadPortletRequest;
import com.liferay.portal.kernel.util.Base64;
import com.liferay.portal.kernel.util.FileUtil;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.util.PortalUtil;

public class AddImageProcess {

	public static boolean addImage(ActionRequest req, Profile profile, String photo) throws Exception {
		
		UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(req);
		boolean flag = validatePhoto(uploadRequest, req, photo);
		if (!flag) {
			return false;
		}
		if (Validator.isNotNull(uploadRequest.getFileName(photo))) {
			if(profile.getPhoto() > 0l){
				ImageReady imageReady = ImageReadyLocalServiceUtil.getService().getImageReady(profile.getPhoto());
				update(uploadRequest, photo, imageReady, false);
			}else {
				ImageReady imageReady = new ImageReadyImpl();
				profile.setPhoto(update(uploadRequest, photo, imageReady, true));
			}
		}
		return true;
	}

	private static long update(UploadPortletRequest uploadRequest, String name, 
			ImageReady photo, boolean add) throws Exception {
		
		File file = uploadRequest.getFile(name);
		ImageBag imageBag = ImageProcessorUtil.read(FileUtil.getBytes(file));
		String contentType = imageBag.getType();
		photo.setImage(Base64.encode(FileUtil.getBytes(file)));
		photo.setDateAdded(new Date());
		photo.setImageType(contentType);
		if (add) {
			ImageReadyLocalServiceUtil.getService().addImageReady(photo);
			return photo.getImageId();
		} else {
			ImageReadyLocalServiceUtil.getService().updateImageReady(photo);
			return 0l;
		}
		
	}

	private static boolean validatePhoto(UploadPortletRequest uploadRequest, ActionRequest req , String photo) {
		HttpServletRequest request = PortalUtil.getHttpServletRequest(req);
		
		if (uploadRequest.getFileName(photo) != "") {
			File file = uploadRequest.getFile(photo);
			try {
				byte[] bytes = FileUtil.getBytes(file);
				ImageBag imageBag = ImageProcessorUtil.read(bytes);
				String type = imageBag.getType();
				int size = bytes.length;
				if (!inAcceptedImageTypes(type)) {
					SessionMessages.add(request, "Invalid-photo");
					return false;
				}
				if (size > Integer.parseInt(PropsUtil.get("users.image.max.size"))) {
					SessionMessages.add(request, "Invalid-size");
					return false;
				}
			} catch (Exception e) {
				//Error
			}
		}
		return true;
	}
	
	 private static boolean inAcceptedImageTypes(String type) {
		 String[] imageType = {"jpg", "jpeg", "gif", "png", "txt"};
		 for (int i=0; i<imageType.length; i++) {
			 if (type.equalsIgnoreCase(imageType[i])) {
				return true;
			 }
		 }
		 return false;
	 }

}